#include <check.h>
#include "test_statistic_info.h"

#include <signal.h>
#include <asm/param.h> /* Para la constante HZ */
#include "statistic_info.h"

/* Testeo de precondiciones */
START_TEST (test_null_path)
{
	StatisticInfo info = statistic_info_new (NULL);
	statistic_info_destroy (info); /* Para evitar un warning */
}
END_TEST

/* Testeo de funcionalidad */

static StatisticInfo info; /* Compartida por los tests... */

START_TEST (test_no_files)
{
	/* Debería andar todo bien si no existen los archivos */
	info = statistic_info_new ("statisticinfo/nofiles");

	/* y obtenemos los defaults */
	fail_unless (info.cpu_user == 0);
	fail_unless (info.cpu_system == 0);
	fail_unless (info.cpu_idle == 0);
	fail_unless (info.context_switches == 0);
	fail_unless (info.process_count == 0);
	fail_unless (info.boot_date == (time_t)0);
	fail_unless (info.disk_requests == 0);
	fail_unless (info.memory_available == 0);
	fail_unless (info.memory_total == 0);
	fail_unless (info.load_average_1 == 0.0);
}
END_TEST
#include <stdio.h>
START_TEST (test_stat)
{
	info = statistic_info_new ("statisticinfo/stat");

	fail_unless (info.cpu_user == (448337+24408) * 1000 / HZ);
	fail_unless (info.cpu_system == 221565 * 1000 / HZ);
	fail_unless (info.cpu_idle == 730218 * 1000 / HZ);
	fail_unless (info.context_switches == 107780343);
	fail_unless (info.process_count == 24492);
	fail_unless (info.boot_date == (time_t)1217968920);
	/* y defaults en el resto... */
	fail_unless (info.disk_requests == 0);
	fail_unless (info.memory_available == 0);
	fail_unless (info.memory_total == 0);
	fail_unless (info.load_average_1 == 0.0);
}
END_TEST

START_TEST (test_diskstats)
{
	info = statistic_info_new ("statisticinfo/diskstats");

	fail_unless (info.disk_requests == 654879+332327);
	/* y defaults en el resto... */
	fail_unless (info.cpu_user == 0);
	fail_unless (info.cpu_system == 0);
	fail_unless (info.cpu_idle == 0);
	fail_unless (info.context_switches == 0);
	fail_unless (info.process_count == 0);
	fail_unless (info.boot_date == (time_t)0);
	fail_unless (info.memory_available == 0);
	fail_unless (info.memory_total == 0);
	fail_unless (info.load_average_1 == 0.0);
}
END_TEST

START_TEST (test_partstat)
{
	info = statistic_info_new ("statisticinfo/partstat");

	/* Al leer datos de particiones hay que dejar el contador en 0. Las
	 * lecturas y escrituras ya estan contadas en estadísticas de disco
	 */
	fail_unless (info.disk_requests == 0);
	/* y defaults en el resto... */
	fail_unless (info.cpu_user == 0);
	fail_unless (info.cpu_system == 0);
	fail_unless (info.cpu_idle == 0);
	fail_unless (info.context_switches == 0);
	fail_unless (info.process_count == 0);
	fail_unless (info.boot_date == (time_t)0);
	fail_unless (info.memory_available == 0);
	fail_unless (info.memory_total == 0);
	fail_unless (info.load_average_1 == 0.0);
}
END_TEST

START_TEST (test_loadavg)
{
	info = statistic_info_new ("statisticinfo/loadavg");

	fail_unless (0.735 < info.load_average_1 &&
	                     info.load_average_1 < 0.745);
	/* y defaults en el resto... */
	fail_unless (info.cpu_user == 0);
	fail_unless (info.cpu_system == 0);
	fail_unless (info.cpu_idle == 0);
	fail_unless (info.context_switches == 0);
	fail_unless (info.process_count == 0);
	fail_unless (info.boot_date == (time_t)0);
	fail_unless (info.disk_requests == 0);
	fail_unless (info.memory_available == 0);
	fail_unless (info.memory_total == 0);
}
END_TEST

START_TEST (test_meminfo)
{
	info = statistic_info_new ("statisticinfo/meminfo");

	fail_unless (info.memory_available == 21108);
	fail_unless (info.memory_total == 970560);
	/* y defaults en el resto... */
	fail_unless (info.cpu_user == 0);
	fail_unless (info.cpu_system == 0);
	fail_unless (info.cpu_idle == 0);
	fail_unless (info.context_switches == 0);
	fail_unless (info.process_count == 0);
	fail_unless (info.boot_date == (time_t)0);
	fail_unless (info.disk_requests == 0);
	fail_unless (info.load_average_1 == 0.0);
}
END_TEST

START_TEST (test_all)
{
	info = statistic_info_new ("statisticinfo/all");

	fail_unless (info.cpu_user == (448337+24408) * 1000 / HZ);
	fail_unless (info.cpu_system == 221565 * 1000 / HZ);
	fail_unless (info.cpu_idle == 730218 * 1000 / HZ);
	fail_unless (info.context_switches == 107780343);
	fail_unless (info.process_count == 24492);
	fail_unless (info.boot_date == (time_t)1217968920);
	fail_unless (info.disk_requests == 654879+332327);
	fail_unless (info.memory_available == 21108);
	fail_unless (info.memory_total == 970560);
	fail_unless (0.735 < info.load_average_1 &&
	                     info.load_average_1 < 0.745);
}
END_TEST

static void teardown (void) {
	statistic_info_destroy (info);
}
/* Armado de la test suite */

Suite *statistic_info_suite (void)
{
	Suite *s = suite_create ("statisticinfo");
	TCase *tc_preconditions = tcase_create ("Precondition");
	TCase *tc_functionality = tcase_create ("Functionality");

	/* Precondiciones */
	tcase_add_test_raise_signal (tc_preconditions, test_null_path, SIGABRT);
	suite_add_tcase (s, tc_preconditions);

	/* Funcionalidad */
	tcase_add_checked_fixture (tc_functionality, NULL, teardown);
	tcase_add_test (tc_functionality, test_no_files);
	tcase_add_test (tc_functionality, test_stat);
	tcase_add_test (tc_functionality, test_diskstats);
	tcase_add_test (tc_functionality, test_partstat);
	tcase_add_test (tc_functionality, test_loadavg);
	tcase_add_test (tc_functionality, test_meminfo);
	tcase_add_test (tc_functionality, test_all);
	/* Falta testear: Números grandes ... */
	suite_add_tcase (s, tc_functionality);

	return s;
}

